package com.lhkj.ct.base.mybatis.core.metadata;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.lhkj.ct.base.utils.LambdaUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
  * <p>
  *     描述：分页排序
  * </p>
  */
public class SqlOrder extends OrderItem {

    public static <T> OrderItem asc(SFunction<T, ?> column) {
        return build(StrUtil.toUnderlineCase(LambdaUtils.getName(column)), true);
    }

    public static <T> OrderItem asc(SFunction<T, ?> column, String alias) {
        return build(StrUtil.toUnderlineCase(LambdaUtils.getName(column, alias)), true);
    }

    public static <T> OrderItem desc(SFunction<T, ?> column) {
        return build(StrUtil.toUnderlineCase(LambdaUtils.getName(column)), false);
    }

    public static <T> OrderItem desc(SFunction<T, ?> column, String alias) {
        return build(StrUtil.toUnderlineCase(LambdaUtils.getName(column, alias)), false);
    }

    public static <T> List<OrderItem> ascs(SFunction<T, ?>... columns) {
        return Arrays.stream(columns).map(SqlOrder::asc).collect(Collectors.toList());
    }

    public static <T> List<OrderItem> descs(SFunction<T, ?>... columns) {
        return Arrays.stream(columns).map(SqlOrder::desc).collect(Collectors.toList());
    }

    private static OrderItem build(String column, boolean asc) {
        return new OrderItem(column, asc);
    }
}
